using System.Collections.Generic;
using System.Linq;

namespace Nice157Advices.Advices
{
    /// <summary>
    /// 26、使用匿名类型储存LINQ查询结果
    /// </summary>
    public static class _26
    {
        public static void Go ()
        {
            var companies = new List<Company>
            {
                new Company { CompanyId = 1, Name = "A" },
                new Company { CompanyId = 2, Name = "B" }
            };

            var persons = new List<Person>
            {
                new Person { PersonId = 1, Name = "capdiem", CompanyId = 1 },
                new Person { PersonId = 2, Name = "lena", CompanyId = 1 },
                new Person { PersonId = 3, Name = "fox", CompanyId = 2 },
                new Person { PersonId = 4, Name = "tomato", CompanyId = 2 },
            };

            var result = from person in persons
                         join company in companies
                         on person.CompanyId equals company.CompanyId
                         select new { PersonName = person.Name, CompanyName =  company.Name };

            foreach (var item in result)
            {
                System.Console.WriteLine($"{item.PersonName} {item.CompanyName}");
            }
        }

        private class Company
        {
            public int CompanyId { get; set; }
            public string Name { get; set; }
        }

        private class Person
        {
            public int PersonId { get; set; }
            public string Name { get; set; }
            public int CompanyId { get; set; }
        }
    }
}